package com.ruirenyun.util.common;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liuchao on 2015/10/19.
 */
public class DateUtil {
    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }
    public static String getNowDateStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(new Date());
    }
    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
    public static String getNowDate(String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }
    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return sdf.format(new Date());
    }

    /**
     * 日期转换成字符串
     * @param d
     * @param format
     * @return str
     */
    public static String dateToStr(Date d, String format){
        if(StringUtils.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);//设置日期格式
        return sdf.format(d);
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str,String format) {

        if(StringUtils.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 格式化日期字符串
     * 比如“2016-4”转换为“2016-04”
     * @param str
     * @param format
     * @return
     */
    public  static String formatDateStr(String str,String format){
        return dateToStr(StrToDate(str,format),format);
    }

    /**
     * 两字符串日期比较大小
     * @param date1
     * @param date2
     * @param format
     * @return
     */
    public  static int compareStrTo(String date1,String date2,String format){
        Date d1 = StrToDate(date1,format);
        Date d2 = StrToDate(date2,format);
        return d1.compareTo(d2);
    }

    /**
     * 计算未来日期
     * @param validity_type
     * @param validity_value
     * @param start_time
     * @return
     */
    public static String calculateFutureDate(String validity_type, String validity_value, String start_time){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try{
            if(start_time == null){
                cal.setTime(new Date());
            }else{
                Date d = sdf.parse(start_time);
                cal.setTime(d);
            }

            if("D".equals(validity_type)){//日
                cal.add(Calendar.DATE, Integer.valueOf(validity_value));
            }else if("M".equals(validity_type)){//月
                cal.add(Calendar.MONTH, Integer.valueOf(validity_value));
            }else if("Y".equals(validity_type)){//年
                cal.add(Calendar.YEAR, Integer.valueOf(validity_value));
            }

            return sdf.format(cal.getTime());
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 纠正错误日期时间 比如 2019-02-30
     * @param start_time
     * @return
     */
    public static String correctDate(String start_time){
        if(start_time != null){
            Calendar cal = Calendar.getInstance();
            String[] dArr = start_time.split("-");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date d = null;
            try {
                d = sdf.parse(dArr[0] + "-" + dArr[1]);
                cal.setTime(d);
                int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                int day = Integer.parseInt(dArr[2]);
                if(day>lastDay){
                    return dArr[0] + "-" + dArr[1] + "-" + lastDay;
                }else{
                    return start_time;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static String calculateFutureDate2(String validity_type, String validity_value, String start_time){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try{
            if(start_time == null){
                cal.setTime(new Date());
            }else{
                start_time = correctDate(start_time);
                Date d = sdf.parse(start_time);
                cal.setTime(d);
            }

            if("D".equals(validity_type)){//日
                cal.add(Calendar.DATE, Integer.valueOf(validity_value));
            }else if("M".equals(validity_type)){//月
                cal.add(Calendar.MONTH, Integer.valueOf(validity_value));
            }else if("Y".equals(validity_type)){//年
                cal.add(Calendar.YEAR, Integer.valueOf(validity_value));
            }

            return sdf.format(cal.getTime());
        }catch (Exception e){
            return "";
        }
    }

    public static boolean isValidDaTe(Date date) {
        long validDay = 30;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        System.out.println(day);
        if (day < 30) {
            return true;
        }
        return false;
    }

    /*
     * 将时间戳转换为时间
     */
    public static Date stampToDate(String s) {
        String res;
        long lt = new Long(s);
        Date date = new Date(lt);
        return date;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampDateToStr(String s) {
        String res;
        long lt = new Long(s);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(lt);
        return df.format(date);
    }

    public static synchronized String getOrderNo(){
        return System.currentTimeMillis() + "";
    }

    /**
     * 判断是否是周末
     * @param dateStr yyyy-MM-dd
     * @return
     */
    public static boolean isWeekend(String dateStr) throws ParseException {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        Date date =sdf.parse(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week=calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(week ==6 || week==0){//0代表周日，6代表周六
            return true;
        }
        return false;
    }

    public static int getMonthSpace(Date date1, Date date2)
            throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        //开始日期若小月结束日期
        int num = 0;
        if(year<0){
            year=-year;
            num = year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);
        }else{
            num = year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
        }
        if(num < 0){
            num = Math.abs(num);
        }
        num = num==0?1:num+1;
        return num;

    }

    public static int getMonthSpace(String date1, String date2)
            throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        //开始日期若小月结束日期
        int num = 0;
        if(year<0){
            year=-year;
            num = year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);
        }else{
            num = year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
        }
        if(num < 0){
            num = Math.abs(num);
        }
        num = num==0?1:num+1;
        return num;

    }

    /**
     * 功能描述 获取两个时间的月份差值 time2 - time1
     *
     * @param time1 时间1
     * @param time2 时间2
     * @return int 月份差
     * @author Deng Ye
     * @date 2021/3/3 11:37
     */
    public static int getDiffMonth(Date time1, Date time2) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(time1);
        int month1 = instance.get(Calendar.MONTH);
        int year1 = instance.get(Calendar.YEAR);
        instance.setTime(time2);
        int month2 = instance.get(Calendar.MONTH);
        int year2 = instance.get(Calendar.YEAR);
        return month2 - month1 + 12 * (year2 - year1);
    }

    public static void main(String[] args) {
//        String str1 = "2018-9-01";
//        String str2 = "2019-9-01";
//        try {
////            System.out.println(getMonthSpace(str1,str2));
//            Date date1 = StrToDate(str1, "yyyy-MM");
//            Date date2 = StrToDate(str2, "yyyy-MM");
//            System.out.println(getMonthSpace(date1,date2));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }


//        System.out.println(dateToStr(new Date(), "yyyy"));
//        System.out.println(dateToStr(new Date(), "MM"));

        String date = "2019-3-30";
        System.out.println(calculateFutureDate2("M", "1" ,date));

    }
}
